package com.tsd.aspect;

import com.tsd.core.annotation.CheckBind;
import com.tsd.core.utils.HlpUtils;
import com.tsd.system.dao.SysGroupPersonMapper;
import com.tsd.system.entity.SysGroupPersonExt;
import com.tsd.system.entity.SysUserExt;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName: CheckBindAspect
 * @Description: TODO
 * @Author: admin
 * @Date: 2022/11/14 11:30
 * @Version: 1.0
 */
@Order(1)
@Aspect
@Component
public class CheckBindAspect {

    @Resource
    private SysGroupPersonMapper sysGroupPersonMapper;

    @Before("@annotation(bind)")
    public void beforeCheckBind(CheckBind bind) {
        SysUserExt userExt = (SysUserExt) SecurityUtils.getSubject().getPrincipal();
        if (bind.group()) {
            if (HlpUtils.isEmpty(userExt.getGroup_sid())) {
                throw new RuntimeException("当前登录用户未绑定部门，请先绑定部门");
            }
        }
        if (bind.person()) {
            if (HlpUtils.isEmpty(userExt.getGroup_person_sid())) {
                throw new RuntimeException("当前登录用户未绑定员工，请先绑定员工");
            }
            SysGroupPersonExt personExt = sysGroupPersonMapper.selectBySid(userExt.getGroup_person_sid());
            if (personExt == null) {
                throw new RuntimeException("当前登录用户未绑定的员工不存在");
            }
        }
    }
}
